package database;

import java.sql.Blob;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.rowset.serial.SerialBlob;

import com.mysql.jdbc.Connection;

public class Database {
	public final Connection con;
	
	private static final String		defaultUrl		= "jdbc:mysql://localhost/";
	private static final String 	defaultDB		= "jitenkanadb";
	private static final String 	defaultUser		= "jitenkana";
	private static final String 	defaultPassword	= "jitenkana";
	
	public Database( String db, String user, String password ) throws SQLException {
		con = (Connection) DriverManager.getConnection(defaultUrl + db + "?useUnicode=true&characterEncoding=utf-8", user, password);
		
		System.out.println("Created database connection!");
		System.out.println("   URL: " + defaultUrl);
		System.out.println("   Database: " + db);
		System.out.println("   User: " + user);
		System.out.println("   Password: " + password);
		
		Statement st = con.createStatement();
		st.executeQuery("SET NAMES 'utf8'");
		//con.setCharacterEncoding("UTF-8");
		//con.setEncoding("UTF-8");
	}
	
	public Database() throws SQLException {
		this( defaultDB, defaultUser, defaultPassword );
	}
	
	public Statement createStatement() {
		try {
			return con.createStatement();
		}
		catch (SQLException e) {
			System.out.println("ERROR: SQLException");
			System.out.println(e.getMessage());
		}
		
		return null;
	}
	
	public static Class<?> getTypeNameType(String name) {
		if (name.contains("int"))
			return Integer.class;
		else if (name.contains("varchar"))
			return String.class;
		else if (name.contains("blob"))
			return SerialBlob.class;
		
		return null;
	}
}
